package com.le.tester.book.concurrentchallenge.synchronizedUtil14;

import java.util.concurrent.TimeUnit;

/**
 * createTime：2025-05-20 13:39
 * description：
 */
public class SleepyBoundedBuffer<V> extends BaseBoundedBuffer<V> {
    public SleepyBoundedBuffer(int size) {
        super(size);
    }

    public void put(V v) throws InterruptedException {

        while (true) {
            synchronized (this) {
                if (!isFull()) {
                    doPut(v);
                    return;
                }
            }
            TimeUnit.MILLISECONDS.sleep(50);
        }
    }

    public V take() throws InterruptedException {
        while (true) {
            synchronized (this) {
                if (!isEmpty()) {
                    return doTake();
                }
            }
            TimeUnit.MILLISECONDS.sleep(50);
        }
    }
}
